package com.qqtech.qquan.mall.controller;

import java.sql.Timestamp;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.qqtech.agent.context.AdminPage;
import com.qqtech.core.common.util.TimeUtil;
import com.qqtech.core.frame.model.PageKit;
import com.qqtech.core.frame.model.PageOrder;
import com.qqtech.core.frame.model.Result;
import com.qqtech.core.frame.service.BaseService;
import com.qqtech.qquan.mall.model.MallSupplierUser;
import com.qqtech.qquan.mall.model.MallSupplierUserQuery;
import com.qqtech.qquan.mall.model.MallSupplierUserVo;
import com.qqtech.qquan.mall.service.MallSupplierUserService;
import com.qqtech.qquan.sys.controller.BaseAgentController;

@Controller
@RequestMapping(value = "/mall/supplier/user")
public class MallSupplierUserController extends BaseAgentController {
	@Resource
	private MallSupplierUserService mallSupplierUserService;
    
	@Override
	protected BaseService<MallSupplierUser> getBaseService() {
		return mallSupplierUserService;
	}
	
	@RequestMapping(value = "/list/{supplierId}", method = RequestMethod.GET)
	public String list(@PathVariable Integer supplierId, HttpServletRequest request, HttpServletResponse response) {
		request.setAttribute("supplierId", supplierId);
		return "/mall/supplier/user-list";
	}
	
	@RequestMapping(value = "/list", method = RequestMethod.POST)
	@ResponseBody
	public AdminPage list(MallSupplierUserQuery query, HttpServletRequest request,
			HttpServletResponse response) {
		AdminPage adminPage = new AdminPage();
		String rows = (String) request.getParameter("rows"); // 每页条数
		query.setSize(Integer.valueOf(rows));
		Pageable pageKit = new PageKit(query.getPage(), query.getSize(),
				new PageOrder(Direction.DESC, "id"));
		Page<MallSupplierUserVo> page = mallSupplierUserService.queryPageList(query, pageKit);
		adminPage.setRows(page.getContent());
		adminPage.setTotal(Integer.valueOf(page.getTotalElements() + ""));
		return adminPage;
	}
	
	@RequestMapping(value = "/add", method = RequestMethod.POST)
	@ResponseBody
	public Result doAdd(MallSupplierUserVo mallSupplierUserVo, HttpServletRequest request,
			HttpServletResponse response) {
		Result ret = new Result();
		if (mallSupplierUserVo == null) {
			ret.addErr("参数不能为空");
		}
		if (ret.checkForm()) {
			MallSupplierUser msu = new MallSupplierUser();
			msu.setSupplierId(mallSupplierUserVo.getSupplierId());
			msu.setUserId(mallSupplierUserVo.getUserId());
			List<MallSupplierUserVo> tempList = mallSupplierUserService.queryList(msu);
			if(tempList != null && tempList.size() > 0){
				ret.addErr("用户已关联");
			}else{
				Timestamp now = TimeUtil.now();
				msu.setNewAt(now);
				mallSupplierUserService.insert(msu);
			}
		}
		return ret;
	}
	
	@RequestMapping(value = "/delete/{id}", method = RequestMethod.POST)
	@ResponseBody
   	public Result doDelete(@PathVariable Integer id) {
		Result ret = new Result();
		if (id == null) {
			ret.addErr("参数不能为空");
		} else{
			mallSupplierUserService.deleteById(id);
		}
   		return ret;
   	}
}
